package cn.com.fuhuitong.main.base

import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import cn.com.fuhuitong.base.BaseAbstractFragment
import cn.com.fuhuitong.base.BaseViewModel
import cn.com.fuhuitong.base.EmptyContent
import cn.com.fuhuitong.base.EmptyViewAdapter
import cn.com.fuhuitong.entity.EmptyState
import cn.com.fuhuitong.entity.MainBean
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.dialog.LoadingDialog
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.alibaba.android.vlayout.layout.FixLayoutHelper
import cn.com.fuhuitong.R
import cn.com.fuhuitong.main.ItemViewType
import com.blankj.utilcode.util.LogUtils

abstract class ViewModeFragment<ViewModel : BaseViewModel> : BaseAbstractFragment() {

    lateinit var viewModel: ViewModel

    abstract fun viewModel(): ViewModel

    override fun doBeforeInit() {
        super.doBeforeInit()
        viewModel = viewModel()
        initViewMode()
    }

    override fun initView() {

    }

    /**
     * 初始化 view mode
     * */
    open fun initViewMode() = Unit

//    lateinit var delegateAdapter: DelegateAdapter
//    fun initViewRecyclerAdapter(
//        view: RecyclerView, viewTypes: Array<Int>, max: Int = 5
//    ) {
//        val viewPool = RecyclerView.RecycledViewPool()
//        view.setRecycledViewPool(viewPool)
//        for (type: Int in viewTypes) {
//            viewPool.setMaxRecycledViews(type, max)
//        }
////        view.isNestedScrollingEnabled = false
//
//        val vLayoutManager = VirtualLayoutManager(requireContext())
//        vLayoutManager.setLayoutViewFactory { ImageView(requireContext()) }
//        view.layoutManager = vLayoutManager
//        delegateAdapter = DelegateAdapter(vLayoutManager, (false))
//        view.adapter = delegateAdapter
//    }
//
//    /**
//     * 初始化空布局
//     * */
//    private var emptyViewAdapter: EmptyViewAdapter? = null
//    fun initEmptyView() {
//        val helper = FixLayoutHelper(FixLayoutHelper.TOP_LEFT, 0, 0)
//        if (emptyViewAdapter == null)
//            emptyViewAdapter = EmptyViewAdapter(requireContext(), helper)
//        delegateAdapter.addAdapter(emptyViewAdapter)
//        emptyViewAdapter?.setOnItemChildClickListener { view, i, mainBean ->
//            if (view.id == R.id.text_empty_execute) {
//                onLoadingEmptyView(mainBean.type)
//            }
//        }
//    }
//
//    open fun onLoadingEmptyView(state: EmptyState) = Unit
//
//    fun isEmptyViewAdapter(): Boolean = emptyViewAdapter == null
//
//    /**
//     * 设置空布局
//     * */
//    fun showLoadEmptyView(emptyContent: EmptyContent = EmptyContent()) {
//        val mainBean = MainBean<EmptyState, EmptyContent>(
//            type = EmptyState.START
//        )
//        mainBean.data = emptyContent
//        emptyViewAdapter?.setAddDataNew(mutableListOf(mainBean))
//    }
//
//    /**
//     * 更新空布局
//     * */
//    fun showErrorEmptyView(emptyContent: EmptyContent = EmptyContent()) {
//        val mainBean = MainBean<EmptyState, EmptyContent>(
//            type = EmptyState.FAILURE
//        )
//        mainBean.data = emptyContent
//        emptyViewAdapter?.setAddDataNew(mutableListOf(mainBean))
//    }
//
//    /**
//     * 更新空布局
//     * */
//    fun showNullEmptyView(emptyContent: EmptyContent = EmptyContent()) {
//        val mainBean = MainBean<EmptyState, EmptyContent>(
//            type = EmptyState.NULL
//        )
//        mainBean.data = emptyContent
//        emptyViewAdapter?.setAddDataNew(mutableListOf(mainBean))
//    }
//
//    /**
//     * 删除空布局
//     * */
//    fun hideEmptyView() {
//        if (emptyViewAdapter != null) {
//            delegateAdapter.removeAdapter(emptyViewAdapter)
//        }
//        emptyViewAdapter = null
//    }


    lateinit var emptyViewAdapter: EmptyViewAdapter
    lateinit var delegateAdapter: DelegateAdapter
    fun initViewRecyclerAdapter(
        recyclerView: RecyclerView, viewTypes: Array<Int>, max: Int = 5
    ) {
//        view.isNestedScrollingEnabled = false

        val vLayoutManager: VirtualLayoutManager = VirtualLayoutManager(requireContext()).apply {
            setLayoutViewFactory { ImageView(requireContext()) }
        }

        with(recyclerView) {
            setRecycledViewPool(RecyclerView.RecycledViewPool().apply {
                setMaxRecycledViews(ItemViewType.EMPTY, max)
                for (type: Int in viewTypes) {
                    setMaxRecycledViews(type, max)
                }
            })
            layoutManager = vLayoutManager
            adapter = DelegateAdapter(vLayoutManager, (false))
                .apply {
                    delegateAdapter = this
                }
        }

        delegateAdapter.addAdapter(
            EmptyViewAdapter(
                requireContext(), FixLayoutHelper(FixLayoutHelper.BOTTOM_LEFT, 0, 0)
            ).apply {
                emptyViewAdapter = this
            })
        emptyViewAdapter.setOnItemChildClickListener { view, _, mainBean ->
            if (view.id == R.id.text_empty_execute) {
                onLoadingEmptyView(mainBean.type)
            }
        }
    }

    open fun onLoadingEmptyView(state: EmptyState) = Unit

    /**
     * 显示加载布局
     * */
    fun showLoadEmptyView(emptyContent: EmptyContent = EmptyContent()) {
        val mainBean = MainBean<EmptyState, EmptyContent>(
            type = EmptyState.START
        )
        mainBean.data = emptyContent
        emptyViewAdapter.setAddDataNew(mutableListOf(mainBean))
    }

    /**
     * 显示异常布局
     * */
    fun showErrorEmptyView(emptyContent: EmptyContent = EmptyContent()) {
        val mainBean = MainBean<EmptyState, EmptyContent>(
            type = EmptyState.FAILURE
        )
        mainBean.data = emptyContent
        emptyViewAdapter.setAddDataNew(mutableListOf(mainBean))
    }

    /**
     * 显示空数据布局
     * */
    fun showNullEmptyView(emptyContent: EmptyContent = EmptyContent()) {
        val mainBean = MainBean<EmptyState, EmptyContent>(
            type = EmptyState.NULL
        )
        mainBean.data = emptyContent
        emptyViewAdapter.setAddDataNew(mutableListOf(mainBean))
    }

    /**
     * 删除空布局
     * */
    fun hideEmptyView() {
        emptyViewAdapter.data.clear()
        emptyViewAdapter.notifyDataSetChanged()
    }




    /**
     * 设置加载对话框
     * */
    private var loadingDialog: LoadingDialog? = null
    private fun showLoadingView(content: String? = null) {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog(requireContext())
        }
        loadingDialog?.showLoading(content)
    }

    /**
     * 删除加载对话框
     * */
    private fun hideLoadingView() {
        loadingDialog?.dismiss()
    }

    /**
     * 处理不同数据状态时页面的显示
     */
    fun handlerResponseLoading(
        status: HttpResponseStatus.Status, errorTip: String, isLoading: Boolean = true
    ) {
        when (status) {
            HttpResponseStatus.Status.START -> if (isLoading) showLoadingView()
            HttpResponseStatus.Status.SUCCESS -> if (isLoading) hideLoadingView()
            HttpResponseStatus.Status.FAILURE -> {
                if (isLoading) hideLoadingView()
                toast(errorTip)
            }
        }
    }

//    /**
//     * 处理不同数据状态时页面的显示
//     */
//    fun handlerResponseEmpty(
//        status: HttpResponseStatus.Status,
//        message: String, length: Int = 0,
//        swipe: SwipeRefreshLayout? = null,
//        emptyContent: EmptyContent = EmptyContent()
//    ) {
//        when (status) {
//            HttpResponseStatus.Status.START -> {
//                if (length == 0) {
//                    if (isEmptyViewAdapter()) {
//                        this.initEmptyView()
//                    }
//                    this.showLoadEmptyView()
//                }
//                swipe?.let {
//                    it.isEnabled = isEmptyViewAdapter()
//                }
//            }
//            HttpResponseStatus.Status.SUCCESS -> {
//
//                if (!isEmptyViewAdapter()) {
//                    if (length == 0) {
//                        showNullEmptyView(emptyContent)
//                    } else {
//                        hideEmptyView()
//                    }
//                }
//                swipe?.let {
//                    it.isEnabled = isEmptyViewAdapter()
//                    it.isRefreshing = false
//                }
//            }
//            HttpResponseStatus.Status.FAILURE -> {
//                if (length == 0 && !isEmptyViewAdapter()) {
//                    showErrorEmptyView(EmptyContent("重新加载", message))
//                }
//                swipe?.let {
//                    it.isEnabled = isEmptyViewAdapter()
//                }
//                toast(message)
//            }
//        }
//    }


    /**
     * 处理不同数据状态时页面的显示
     */
    fun handlerResponseEmpty(
        status: HttpResponseStatus.Status,
        message: String, length: Int = 0,
        swipe: SwipeRefreshLayout? = null,
        emptyContent: EmptyContent = EmptyContent()
    ) {
        when (status) {
            HttpResponseStatus.Status.START -> {
                if (length == 0) {
                    this.showLoadEmptyView()
                }
            }
            HttpResponseStatus.Status.SUCCESS -> {
                if (length == 0) {
                    this.showNullEmptyView(emptyContent)
                } else {
                    this.hideEmptyView()
                }
            }
            HttpResponseStatus.Status.FAILURE -> {
                if (length == 0) {
                    this.showErrorEmptyView(EmptyContent("重新加载", message))
                }
                swipe?.let {
                    it.isRefreshing = false
                }
                toast(message)
            }
        }
    }
}